<?php

namespace app\models;

use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "shop".
 *
 * @property string $shop_id 商品表主键，递增
 * @property string $shop_name 商品名称
 * @property string $shop_desc 商品简单描述
 * @property string $shop_keyword 商品关键字
 * @property string $shop_type 商品类型
 * @property int $shop_state 发布状态 1.新品发布 2.限时促销 3.特价处理
 * @property string $shop_original 商品原价
 * @property string $shop_present 商品现价
 * @property int $shop_num 商品数量
 * @property string $shop_begintime 商品开始添加时间
 * @property string $shop_endtime 商品结束时间
 * @property string $shop_norms 设置规格
 * @property int $shop_frame 商品是否上架
 * @property int $shop_speak 商品是否允许评论
 * @property string $shop_img 商品图片
 * @property string $shop_content 内容介绍
 */
class Shop extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'shop';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['shop_original', 'shop_present'], 'number'],
            [['shop_begintime', 'shop_endtime'], 'safe'],
            [['shop_content'], 'string'],
            [['shop_name'], 'string', 'max' => 100],
            [['shop_desc', 'shop_norms'], 'string', 'max' => 60],
            [['shop_keyword', 'shop_type'], 'string', 'max' => 50],
            [['shop_state', 'shop_frame', 'shop_speak'], 'string', 'max' => 1],
            [['shop_num'], 'string', 'max' => 5],
            [['shop_img'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
        ];
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        if ($this->validate()) {
            $this->shop_img->saveAs('upload/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'shop_id' => 'Shop ID',
            'shop_name' => 'Shop Name',
            'shop_desc' => 'Shop Desc',
            'shop_keyword' => 'Shop Keyword',
            'shop_type' => 'Shop Type',
            'shop_state' => 'Shop State',
            'shop_original' => 'Shop Original',
            'shop_present' => 'Shop Present',
            'shop_num' => 'Shop Num',
            'shop_begintime' => 'Shop Begintime',
            'shop_endtime' => 'Shop Endtime',
            'shop_norms' => 'Shop Norms',
            'shop_frame' => 'Shop Frame',
            'shop_speak' => 'Shop Speak',
            'shop_img' => 'Shop Img',
            'shop_content' => 'Shop Content',
        ];
    }

    /**
     *
     *   分类查看
     */
    public function types(){
        $type_arr = Yii::$app->db->createCommand("SELECT * FROM type")->queryAll();
        $data = $this->getTree($type_arr,$pid=0);
        return $data;
    }

    /**
     *  递归方法实现
     * 1
     */
    public function getTree($data,$pid=0){
        $arr=[];
        foreach($data as $key=>$v){
            if($v['type_pid']==$pid){
                $arr[$key] = $v;
                $arr[$key]['son'] = $this->getTree($data,$v['type_id']);
            }
            if(empty($arr[$key]['son'])){
                unset($arr[$key]['son']);
            }
        }
        return $arr;
    }

    public function shop_arr($type_id){
        $where = '1=1';
        if(!empty($type_id)){
            $where .=" AND type_id = $type_id";
        }

        $data = Yii::$app->db->CreateCommand("SELECT * from shop as s JOIN type as t on s.shop_type = t.type_id where $where")->queryAll();
        return $data;
    }

}













































